package editer;

import haxe.ui.events.UIEvent;
import haxe.ui.containers.VBox;
import haxe.io.Path;
import haxe.ui.containers.Box;

/**
 * UI编辑器
 */
@:build(haxe.ui.macros.ComponentMacros.build("assets/view-editer.xml"))
class ViewEditer extends VBox {
	public var files:Map<String, EditFileView> = [];

	/**
	 * 启动一个新的编辑器
	 * @param path 文件路径
	 * @param preview 是否仅预览
	 */
	public function addEidtView(path:String, preview:Bool = true):Void {
		if (files.exists(path)) {
			var view = files.get(path);
			if (view.parentComponent != null) {
				if (view.preview)
					view.preview = preview;
				this.tabview.selectedPage = view;
				return;
			}
		}
		// 移除所有预览视图
		for (key => view in files) {
			if (view.preview || view.parentComponent == null) {
				tabview.removeComponent(view);
				files.remove(key);
			}
		}
		var view = new EditFileView(path);
		view.preview = preview;
		this.tabview.addComponent(view);
		this.tabview.selectedPage = view;
		files.set(path, view);
	}

	override function onInitialize() {
		super.onInitialize();
		tabview.onChange = (ui:UIEvent) -> {
			// TOOD 需要在预览中渲染
			MainView.current.perview(cast tabview.selectedPage);
		}
	}
}
